<template>
  <el-container>
    <!-- 头部区域 -->
    <el-header>
      <div>
        <img src="../assets/logo.png" alt="" />
        <span>后台管理程序</span>
      </div>
      <div><el-avatar icon="el-icon-user-solid"></el-avatar>      <el-button type="info" @click="logout">退出</el-button></div>
    </el-header>
    <!-- 页面主体区域 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <!-- 放置一个合并展开的按钮 -->
        <div class="toggle-button" @click="toggleCollapse"><i class="el-icon-menu"></i></div>
        <!-- 菜单开始 -->
        <el-menu
      :default-active="activePath"
      class="el-menu-vertical-demo"
      @open="handleOpen"
      @close="handleClose"
      background-color="#2980b9"
      text-color="#fff"
      active-text-color="#ffd04b" unique-opened
      :collapse = isCollapse
      :collapse-transition="false"
      router
      ><!-- unique-opened：是否只保持一个子菜单的展开 :collapse-transition="false" 关闭动画效果-->
      <!-- 一级菜单 -->
      <el-submenu :index="item.id.toString()" v-for = "item in menuList" :key="item.id">
        <!-- 一级菜单模板区域 -->
        <template slot="title">
          <!-- 图标 -->
          <i class="el-icon-set-up"></i>
          <!-- 文本 -->
          <span>{{item.authName}}</span>
        </template>
        <!-- 二级菜单 -->
            <el-menu-item :index="'/'+subItem.path" v-for="subItem in item.children" :key="subItem.id" @click="saveNavState('/'+subItem.path)">
              <!-- 图标 -->
            <i class="el-icon-menu"></i>
            <span>{{subItem.authName}}</span>
            </el-menu-item>
      </el-submenu>
    </el-menu>
        </el-aside>
      <!-- 右侧内容主体 -->
      <el-main>
        <!-- 路由占位符 -->
        <router-view></router-view>
        </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data () {
    return {
      isCollapse: false, // 菜单是否折叠
      // 左侧菜单数据
      menuList: [],
      // 激活左侧菜单项
      activePath: ''
    }
  },
  created () { // 在页面创建时获取左侧菜单数据
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath') // 从本地获取左侧菜单激活状态
  },
  methods: {
    handleOpen (key, keyPath) {
      console.log(key, keyPath)
    },
    handleClose (key, keyPath) {
      console.log(key, keyPath)
    },
    logout () {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 获取左侧菜单数据
    async getMenuList () {
      const { data: res } = await this.$http.get('menus') // 把请求回来的数据只取里边的data部分（服务器返回的真实数据），Axios自己封装的数据不要
      this.menuList = res.data // 赋值给data中的menuList数组
      // console.log(res)
    },
    // 点击左侧合并展开按钮
    toggleCollapse () {
      this.isCollapse = !this.isCollapse
    },
    saveNavState (activePath) {
      window.sessionStorage.setItem('activePath', activePath) // 保存activePath状态到本地
      this.activePath = activePath
    }
  }
}
</script>

<style lang="less" scoped>
.el-container {
  height: 100%;
}
.el-header {
  background-color: #2980b9;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #fff;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center; // 上下距中
    span {
      margin-left: 15px;
    }
    img {
      height: 55px;
      width: 55px;
      border-radius: 50%;
      background-color: #eee;
    }
  }
  .el-avatar{
    margin: 0 15px;
  }
}
//左侧菜单样式
.el-aside {
  background-color: #2980b9;
}
.el-menu-vertical-demo:not(.el-menu--collapse) {
  width: 200px;
  min-height: 400px;
}
.el-main {
  background-color: #E9EDF1;
}
.toggle-button {
  color: azure;
  font-size: 20px;
  text-align: center;
  cursor: pointer;
}
</style>
